/**
 * @Author 韦建韩
 * @Date 2022/5/15
 */
package com.example.devops.Manage;

import com.example.devops.dao.ProjectMapper;
import com.example.devops.entity.pojo.Project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class HttpPortCache {
    @Autowired
    ProjectMapper projectMapper;
    private static List<String> port = Arrays.asList("8900","8901","8902","8903","8904","8905","8906","8907","8908","8909","8910","8911");
    private static Map<String, Boolean> m = new HashMap<>();
    public Map getPortMap(){
        return m;
    }
    public List getPortList(){
        return port;
    }

//    @PostConstruct
    public  void init(){
        List<Project> list = projectMapper.selectList(null);
        List<String> ports = list.stream().filter(x->Objects.nonNull(x.getPort())).map(x -> x.getPort()).collect(Collectors.toList());
        for (String port : ports) {
            m.put(port, true);
        }
    }
   synchronized public static String getPort(){
       for (String s : port) {
           Boolean isUsed = m.get(s);
           if(Objects.isNull(isUsed) || !isUsed){
               return s;
           }
       }
       return null;
   }

}
